var target : Transform;
var toFade : GameObject ; 
var targetHeight : float= 1.7;
var distance : float = 5.0;
var maxDistance : float = 20; 
var minDistance : float = .6; 
var xSpeed : float = 250.0; 
var ySpeed : float = 120.0; 

var startFade : float = 5;
var stopFade : float = 1.0;
var yMinLimit : int = -80; 
var yMaxLimit : int= 80; 

var zoomRate : int= 40; 

var rotationDampening : float = 3.0; 
var zoomDampening : float = 5.0; 

var x : float  = 0.0; 
var y : float = 0.0; 
var currentDistance : float ; 
var desiredDistance : float ; 
var correctedDistance : float ; 

function disableWoW(){
	enabled = false;
}	
function enableWoW(){
	enabled = true;
}
function Start () 
{ 
	if(!toFade.renderer)
		Debug.Log("WOWCamera: The toFade object has no renderer.");
		
	//Vector3 angles = transform.eulerAngles; 
	x = -180;//angles.x; 
	y = 20;//angles.y; 

	currentDistance = distance; 
	desiredDistance = distance; 
	correctedDistance = distance; 

	// Make the rigid body not change rotation 
	if (rigidbody) 
		rigidbody.freezeRotation = true; 
} 

/** 
 * Camera logic on LateUpdate to only update after all character movement logic has been handled. 
 */ 
function OnMouseDown () {
   Debug.Log("DuPA");
	// Lock the cursor
}
function LateUpdate () 
{ 
   // Don't do anything if target is not defined 
	if (!target) 
		return; 
	//Screen.showCursor = true;
	Screen.lockCursor = false;

	// If either mouse buttons are down, let the mouse govern camera position 
	if (/*Input.GetMouseButton(0) ||*/ Input.GetMouseButton(1)) 
	{ 
		//Screen.showCursor = false;
		Screen.lockCursor = true;
		x += Input.GetAxis("Mouse X") * xSpeed * 0.02; 
		y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02; 
	} 
	// otherwise, ease behind the target if any of the directional keys are pressed 
	else if (Input.GetAxis("Vertical") != 0 || Input.GetAxis("Horizontal") != 0) 
	{ 
		var targetRotationAngle : float= target.eulerAngles.y; 
		var currentRotationAngle : float = transform.eulerAngles.y; 
		x = Mathf.LerpAngle(currentRotationAngle, targetRotationAngle, rotationDampening * Time.deltaTime); 
	} 

	y = ClampAngle(y, yMinLimit, yMaxLimit); 

	// set camera rotation 
	var rotation : Quaternion= Quaternion.Euler(y, x, 0); 
	
	// calculate the desired distance 
	desiredDistance -= Input.GetAxis("Mouse ScrollWheel") * Time.deltaTime * zoomRate * Mathf.Abs(desiredDistance); 
	desiredDistance = Mathf.Clamp(desiredDistance, minDistance, maxDistance); 
	correctedDistance = desiredDistance; 
	
		
	// calculate desired camera position 
	var position : Vector3 = target.position - (rotation * Vector3.forward * desiredDistance + new Vector3(0, -targetHeight, 0)); 

	// check for collision using the true target's desired registration point as set by user using height 
	var collisionHit : RaycastHit; 
	var trueTargetPosition: Vector3= new Vector3(target.position.x, target.position.y + targetHeight, target.position.z); 

	// if there was a collision, correct the camera position and calculate the corrected distance 
	var isCorrected:boolean= false; 
	if (Physics.Linecast(trueTargetPosition, position, collisionHit))
	{ 
		position = collisionHit.point; 
		correctedDistance = Vector3.Distance(trueTargetPosition, position); 
		isCorrected = true; 
	} 
	
	// For smoothing, lerp distance only if either distance wasn't corrected, or correctedDistance is more than currentDistance 
	currentDistance = !isCorrected || correctedDistance > currentDistance ? Mathf.Lerp(currentDistance, correctedDistance, Time.deltaTime * zoomDampening) : correctedDistance; 
	
	//calculate the fade
	toFade.renderer.material.color.a = (currentDistance<startFade)?((currentDistance-stopFade)/startFade):5;
	toFade.renderer.enabled = currentDistance > stopFade;
	
	// recalculate position based on the new currentDistance 
	position = target.position - (rotation * Vector3.forward * currentDistance + new Vector3(0, -targetHeight, 0)); 

	transform.rotation = rotation; 
	transform.position = position; 
} 

function ClampAngle(angle:float, min:float, max:float) { 
	if (angle < -360) 
		angle += 360; 
	if (angle > 360) 
		angle -= 360; 
	return Mathf.Clamp(angle, min, max); 
} 